package com.bcu.wish.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.bcu.wish.config.GlobalConfig;
import com.bcu.wish.entity.Message;
import com.bcu.wish.entity.User;
import com.bcu.wish.entity.search.WishfillSearch;
import com.bcu.wish.mapper.UserMapper;
import com.bcu.wish.service.WishfillService;
import com.deepoove.poi.XWPFTemplate;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.bcu.wish.mapper.WishfillMapper;
import com.bcu.wish.entity.Wishfill;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

@Service
public class WishfillServiceImpl implements WishfillService {

    @Resource
    private WishfillMapper wishfillMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private GlobalConfig globalConfig;

    @Override
    public Message deleteByPrimaryKey(Integer wishFillId) {
        return wishfillMapper.deleteByPrimaryKey(wishFillId)>0? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(Wishfill record) {

        return wishfillMapper.insert(record)>0? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Wishfill record) {
        Wishfill wishfill = wishfillMapper.selectByUserId(record.getUserId());
        if (wishfill!=null){
            return Message.success("该志愿信息已存在！");
        }else
        return wishfillMapper.insertSelective(record)>0? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<Wishfill> selectByPrimaryKey(Integer wishFillId) {
        return Message.success(null).add(wishfillMapper.selectByPrimaryKey(wishFillId));
    }

    @Override
    public Message updateByPrimaryKeySelective(Wishfill record) {
        return wishfillMapper.updateByPrimaryKeySelective(record)>0? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Wishfill record) {
        return wishfillMapper.updateByPrimaryKey(record)>0? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<List<Wishfill>> selectAll() {
        return Message.success(null).add(wishfillMapper.selectAll());
    }

    @Override
    public Message<Wishfill> selectByUserId(String userId) {
        return Message.success(null).add(wishfillMapper.selectByUserId(userId));
    }

    @Override
    public Message getExcel() {
        List<Wishfill> wishfillList = wishfillMapper.selectAll();
        ArrayList rows = new ArrayList<>();

        for (int i=0;i<wishfillList.size();i++)
        {
            Wishfill wishfill =wishfillList.get(i);
            Map<String,Object> row =new LinkedHashMap<>();
            row.put("学号",wishfill.getUserId());
            row.put("学生姓名",wishfill.getUserinfo().getUserinfoUserName());
            row.put("第一志愿",wishfill.getWishFirst());
            row.put("第二志愿",wishfill.getWishSecond());
            row.put("第三志愿",wishfill.getWishThird());
            row.put("第四志愿",wishfill.getWishFourth());
            row.put("第五志愿",wishfill.getWishFifth());
            rows.add(row);
        }
        ArrayList<Map<String,Object>> allrows = CollUtil.newArrayList(rows);
        // 通过工具类创建writer
        System.out.println(globalConfig.filePath);
        ExcelWriter writer = ExcelUtil.getWriter(globalConfig.filePath+"wish.xlsx");
        // 合并单元格后的标题行，使用默认标题样式
        //writer.merge(rows.size() - 1, "志愿填报");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(allrows, true);
        // 关闭writer，释放内存
        writer.close();
        String returnUrl=globalConfig.hostUrl+"resource/"+"wish.xlsx";

        return Message.success(null).add(returnUrl);
    }

    @Override
    public Message getWord(String userId) throws IOException {
        Wishfill wishfill = wishfillMapper.selectByUserId(userId);

        ClassPathResource classPathResource= new ClassPathResource("moban.docx");
        File moban = classPathResource.getFile();
        String reportPath = null;
        try {
            reportPath = moban.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String outputName = wishfill.getUserinfo().getUserinfoUserId()+" 填报表.docx";
        Date d = new Date();
        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        String date = df.format(d);
        //将信息存入Map
        Map<String, Object> datas = new HashMap<String, Object>() {
            {
                put("name",wishfill.getUserinfo().getUserinfoUserName());
                put("number",wishfill.getUserId());
                put("idcard",wishfill.getUserinfo().getUserinfoUserIdcard());
                put("wishFirst",wishfill.getWishFirst());
                put("wishSecond",wishfill.getWishSecond());
                put("wishThird",wishfill.getWishThird());
                put("wishFourth",wishfill.getWishFourth());
                put("wishFifth",wishfill.getWishFifth());
                put("date",date);
            }
        };

        XWPFTemplate template = XWPFTemplate.compile(reportPath)
                .render(datas);
        FileOutputStream out;
        try {
            out = new FileOutputStream(globalConfig.filePath+outputName);
            template.write(out);
            out.flush();
            out.close();
            template.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Message.success(null).add(globalConfig.hostUrl+"resource/"+outputName);
    }

    @Override
    public Message<List<User>> StudentDivided() {
        userMapper.updateUserResult();
        int english = 143;
        int translation = 35;
        int german = 35;
        int french = 70;
        int spanish = 70;
        String e = "英语";
        String t = "翻译";
        String g = "德语";
        String f = "法语";
        String s = "西语";
        List<Wishfill> efirst;
        List<Wishfill> esecond;
        List<Wishfill> ethird;
        List<Wishfill> efourth;
        List<Wishfill> efifth;
        //各专业第一志愿
        efirst = first(e);
        esecond = first(t);
        ethird = first(g);
        efourth = first(f);
        efifth = first(s);
        //数量
        int enums=0;
        int tnums=0;
        int gnums=0;
        int fnums=0;
        int snums=0;
        //英语
        for (int i=0;i<efirst.size() && i<english;i++) {
            Wishfill w = efirst.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), e);
            enums++;
        }
        //翻译
        for (int i=0;i<esecond.size() && i<translation;i++) {
            Wishfill w = esecond.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), t);
            tnums++;
        }
        //德语
        for (int i=0;i<ethird.size() && i<german;i++) {
            Wishfill w = ethird.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), g);
            gnums++;
        }
        //法语
        for (int i=0;i<efourth.size() && i<french;i++) {
            Wishfill w = efourth.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), f);
            fnums++;
        }
        //西语
        for (int i=0;i<efifth.size() && i<spanish;i++) {
            Wishfill w = efifth.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), s);
            snums++;
        }
        //第二志愿
        efirst = second(e);
        esecond = second(t);
        ethird = second(g);
        efourth = second(f);
        efifth = second(s);
        //数量
        int enums2=0;
        int tnums2=0;
        int gnums2=0;
        int fnums2=0;
        int snums2=0;
        //英语
        for (int i=0;i<english - enums&& i<efirst.size();i++) {
            Wishfill w = efirst.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), e);
            enums2++;
        }
        //翻译
        for (int i=0;i<translation - tnums&&i<esecond.size();i++) {
            Wishfill w = esecond.get(i);
            System.out.println(translation - first(t).size()+" "+w.getUserId()+" "+translation);
            userMapper.updateUserResultByUserId(w.getUserId(), t);
            tnums2++;
        }
        //德语
        for (int i=0;i<german-gnums&&i<ethird.size();i++) {
            Wishfill w = ethird.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), g);
            gnums2++;
        }
        //法语
        for (int i=0;i<french - fnums&&i<efourth.size();i++) {
            Wishfill w = efourth.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), f);
            fnums2++;
        }
        //西语
        for (int i=0;i<spanish -snums&&i<efifth.size();i++) {
            Wishfill w = efifth.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), s);
            snums2++;
        }
        //第三志愿
        efirst = third(e);
        esecond = third(t);
        ethird = third(g);
        efourth = third(f);
        efifth = third(s);
        //数量
        int enums3=0;
        int tnums3=0;
        int gnums3=0;
        int fnums3=0;
        int snums3=0;
        //英语
        for (int i=0;i<english - enums-enums2&&i<efirst.size();i++) {
            Wishfill w = efirst.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), e);
            enums3++;
        }
        //翻译
        for (int i=0;i<translation - tnums-tnums2&&i<esecond.size();i++) {
            Wishfill w = esecond.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), t);
            enums3++;
        }
        //德语
        for (int i=0;i<german-gnums-gnums2&&i<ethird.size();i++) {
            Wishfill w = ethird.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), g);
            enums3++;
        }
        //法语
        for (int i=0;i<french - fnums-fnums2&&i<efourth.size();i++) {
            Wishfill w = efourth.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), f);
            enums3++;
        }
        //西语
        for (int i=0;i<spanish -snums-snums2&&i<efifth.size();i++) {
            Wishfill w = efifth.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), s);
            enums3++;
        }
        //第四志愿
        efirst = fourth(e);
        esecond = fourth(t);
        ethird = fourth(g);
        efourth = fourth(f);
        efifth = fourth(s);
        //数量
        int enums4=0;
        int tnums4=0;
        int gnums4=0;
        int fnums4=0;
        int snums4=0;
        //英语
        for (int i=0;i<english - enums-enums2-enums3&&i<efirst.size();i++) {
            Wishfill w = efirst.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), e);
            enums4++;
        }
        //翻译
        for (int i=0;i<translation - tnums-tnums2-tnums3&&i<esecond.size();i++) {
            Wishfill w = esecond.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), t);
            tnums4++;
        }
        //德语
        for (int i=0;i<german-gnums-gnums2-gnums3&&i<ethird.size();i++) {
            Wishfill w = ethird.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), g);
            gnums4++;
        }
        //法语
        for (int i=0;i<french - fnums-fnums2-fnums3&&i<efourth.size();i++) {
            Wishfill w = efourth.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), f);
            fnums4++;
        }
        //西语
        for (int i=0;i<spanish -snums-snums2-snums3&&i<efifth.size();i++) {
            Wishfill w = efifth.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), s);
            snums4++;
        }
        //第五志愿
        efirst = fifth(e);
        esecond = fifth(t);
        ethird = fifth(g);
        efourth = fifth(f);
        efifth = fifth(s);
        //英语
        for (int i=0;i<english - enums-enums2-enums3-enums4&&i<efirst.size();i++) {
            Wishfill w = efirst.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), e);
        }
        //翻译
        for (int i=0;i<translation-tnums-tnums2-tnums3-tnums4&&i<esecond.size();i++) {
            Wishfill w = esecond.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), t);
        }
        //德语
        for (int i=0;i<german-gnums-gnums2-gnums3-gnums4&&i<ethird.size();i++) {
            Wishfill w = ethird.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), g);
        }
        //法语
        for (int i=0;i<french-fnums-fnums2-fnums3-fnums4&&i<efourth.size();i++) {
            Wishfill w = efourth.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), f);
        }
        //西语
        for (int i=0;i<spanish-snums-snums2-snums3-snums4&&i<efifth.size();i++) {
            Wishfill w = efifth.get(i);
            userMapper.updateUserResultByUserId(w.getUserId(), s);
        }
        return Message.success(null);
    }

    @Override
    public Message<List<Wishfill>> lalalal(String s){
        return Message.success(null).add(wishfillMapper.selectByWishFirst(s));
    }

    @Override
    public Message search(WishfillSearch wishfillSearch) {
        return Message.success(null).add(wishfillMapper.search(wishfillSearch));
    }

    public List<Wishfill> first(String wishFirst){
        return wishfillMapper.selectByWishFirst(wishFirst);
    }

    public List<Wishfill> second(String wishSecond){
        return wishfillMapper.selectByWishSecond(wishSecond);
    }

    public List<Wishfill> third(String wishThird){
        return wishfillMapper.selectByWishThird(wishThird);
    }

    public List<Wishfill> fourth(String wishFourth){
        return wishfillMapper.selectByWishFourth(wishFourth);
    }

    public List<Wishfill> fifth(String wishFifth){
        return wishfillMapper.selectByWishFifth(wishFifth);
    }

}

